package mvc.framework;

import javax.servlet.http.HttpSession;

/**
 * Classe che contiene i metodi per manipolare la sessione 
 * 
 * @author Grillo
 */
public class SessionManager {
    public static final String SESSION_RQST_HANDLER = "rqst_handler";
    public static final String SESSION_VIEW = "currentView";
    public static final String SESSION_ERROR_MSG = "errorMsg";
    public static final String SESSION_CONFIRM_MSG = "confirmMsg";

    public static IRequestHandler getRequestHandler(HttpSession session) {
        return (IRequestHandler) session.getAttribute(SESSION_RQST_HANDLER);
    }

    public static void setRequestHandler(HttpSession session, IRequestHandler requestHandler) {
        session.setAttribute(SESSION_RQST_HANDLER, requestHandler);
    }

    public static String getErrorMessage(HttpSession session) {
        return (String) session.getAttribute(SESSION_ERROR_MSG);
    }

    public static void setErrorMessage(HttpSession session, String errorMessage) {
        session.setAttribute(SESSION_ERROR_MSG, errorMessage);
    }

    public static String removeErrorMessage(HttpSession session) {
        String result = getErrorMessage(session);
        session.removeAttribute(SESSION_ERROR_MSG);
        return result;
    }

    public static String getConfirmMessage(HttpSession session) {
        return (String) session.getAttribute(SESSION_CONFIRM_MSG);
    }

    public static void setConfirmMessage(HttpSession session, String confirmMessage) {
        session.setAttribute(SESSION_CONFIRM_MSG, confirmMessage);
    }

    public static String removeConfirmMessage(HttpSession session) {
        String result = getConfirmMessage(session);
        session.removeAttribute(SESSION_CONFIRM_MSG);
        return result;
    }

    // Metodi generici
    public static void setAttribute(HttpSession session, String attribKey, Object attribObj) throws IllegalStateException {
        session.setAttribute(attribKey, attribObj);
    }

    public static Object getAttribute(HttpSession session, String attribKey) throws IllegalStateException {
        return session.getAttribute(attribKey);
    }

    public static void removeAttribute(HttpSession session, String attribKey) throws IllegalStateException {
        session.removeAttribute(attribKey);
    }

    public static Object getAndRemoveAttribute(HttpSession session, String attribKey) throws IllegalStateException {
        Object obj = session.getAttribute(attribKey);
        session.removeAttribute(attribKey);
        return obj;
    }
}